<?php
class Admin_Model_Lops {
	private $lop;
	
	function __construct(){
		$this->lop = new Admin_Model_DbTable_Lops();
	}
	
	/**
	 * 
	 * Lay danh sach thong tin ve lop
	 * @param unknown_type $where: dieu kien loc
	 */
	public  function listLop($where =null, $order = 'id desc'){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$tbSelect = new Zend_Db_Select($dbAdapter);
		
		$tbSelect->from(
					array('l'=>'lop'),
					array('*') //xem tat ca cac cot
				); 
				
		if($where != null){
			$tbSelect->where($where);
		}
		
		$tbSelect->order('id desc');
				
		return $tbSelect;
	}
	
	public  function listLopManager($where =null, $order = 'id desc'){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$tbSelect = new Zend_Db_Select($dbAdapter);
		
		$tbSelect->from(array('l'=>'lop'),array('*'))
				->joinInner(array('ql'=>'quanly'), 'ql.idLop = l.id','*');
				
		if($where != null){
			$tbSelect->where($where);
		}
		
		$tbSelect->order('id desc');
				
		return $tbSelect;
	}
	
	/**
	 * 
	 * Them vao bang lop
	 * @param unknown_type $ten: ten cua lop
	 * @param unknown_type $khoa: khóa học
	 */
	public function addLop($id, $ten,$khoa){
		$data = array(
			'id'		=> $id,
			'tenLop'	=>	$ten,
			'khoa'		=>	$khoa
		);
		$this->lop->insert($data);
	}
	
	/**
	 * 
	 * Delete record cua bang lop
	 * @param unknown_type $ten
	 * @param unknown_type $khoa
	 * @param unknown_type $id
	 */
	public function editLop($ten,$khoa,$id){
		$data = array(
			'tenLop'	=>	$ten,
			'khoa'	=> 	$khoa
		);
		$this->lop->update($data, "id = '".$id."'");
	}
	
	/**
	 * 
	 * Xoa record
	 * @param unknown_type $id: ma lop
	 */
	public function deleteLop($id){
		$this->lop->delete("id = '".$id."'");
	}
	
	/**
	 * 
	 * lay 1 dong du lieu
	 * @param unknown_type $id
	 * @return $row: la 1 mang cac info
	 */
	public function fetch($id){
			return  $this->lop->fetchRow("id = '".$id."'");	
	}
	
	/**
	 * 
	 * Lay tat ca cac khoa hoc
	 */
	public function getKhoaHoc(){
		$dbApdapter = Zend_Db_Table::getDefaultAdapter();
		$tbSelect =	$dbApdapter->fetchAll("select distinct khoa from lop ORDER BY khoa");
		
		return $tbSelect;				
	}
	
	/**
	 * 
	 * Get du lieu de export ra file excel
	 */
	function getDataLopToExport(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$dbSelect = new Zend_Db_Select($dbAdapter);
		$dbSelect->from('lop');
		
		return $dbSelect->query()->fetchAll();
	}
	
	public function getAllLop($where=NULL){
		return $this->lop->fetchAll($where);
		
	}
}